﻿/*
 * Copyright (c) 2006 by Windward Studios, Inc. All rights reserved.
 *
 * This software is the confidential and proprietary information of Windward Studios ("Confidential Information").  
 * You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the 
 * license agreement you entered into with Windward Studios, Inc.
 */

using System;
using Kailua.net.windward.utils.datetime;
using NUnit.Framework;


namespace net.windward.utils.datetime
{
	/// <summary>
	/// Test the TimeZone classes
	/// </summary>
	[TestFixture]
	public class TestTimeZone
	{
		[SetUp]
		public void Init()
		{
		}

		[TearDown]
		public void TearDown()
		{
		}

		/// <summary>
		/// Tests timezones. This assumes running in Mountain time zone.
		/// </summary>
		[Test]
		public void TestAll()
		{
			DateTime now = DateTime.Now;

			Assert.AreEqual("Mountain Standard Time", TimeZone.CurrentTimeZone.StandardName);

			TimeZoneInfo tz = TimeZoneInfo.Local;
			Assert.AreEqual("Mountain Standard Time", tz.Id);
			Assert.AreEqual(tz.IsDaylightSavingTime(now) ? -360 : -420, tz.GetUtcOffset(now).TotalMinutes);

			string key = tz.Id;

			tz = TimeZoneInfo.FindSystemTimeZoneById(key);
			Assert.AreEqual("Mountain Standard Time", tz.StandardName);
			Assert.AreEqual(tz.IsDaylightSavingTime(now) ? -360 : -420, tz.GetUtcOffset(now).TotalMinutes);

			tz = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
			Assert.AreEqual("Eastern Standard Time", tz.StandardName);
			Assert.AreEqual(tz.IsDaylightSavingTime(now) ? -240 : -300, tz.GetUtcOffset(now).TotalMinutes);

			tz = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
			Assert.AreEqual("GMT Standard Time", tz.StandardName);
			Assert.AreEqual(tz.IsDaylightSavingTime(now) ? 60 : 0, tz.GetUtcOffset(now).TotalMinutes);

			// check time conversions - not daylight
			tz = TimeZoneInfo.Local;
			DateTime dt = new DateTime(2008, 1, 2, 13, 4, 5, DateTimeKind.Utc);
			DateTime cnv = TimeZoneInfo.ConvertTimeToUtc(dt);
			Assert.IsFalse(tz.IsDaylightSavingTime(dt));
			Assert.AreEqual(13, cnv.Hour);
			Assert.AreEqual(4, cnv.Minute);
			Assert.AreEqual(5, cnv.Second);
			Assert.AreEqual(DateTimeKind.Utc, cnv.Kind);

			cnv = TimeZoneInfo.ConvertTimeFromUtc(dt, tz);
			Assert.AreEqual(13 - 7, cnv.Hour);
			Assert.AreEqual(4, cnv.Minute);
			Assert.AreEqual(5, cnv.Second);
			Assert.AreEqual(DateTimeKind.Unspecified, cnv.Kind);

			// daylight
			tz = TimeZoneInfo.Local;
			dt = new DateTime(2008, 5, 2, 13, 4, 5, DateTimeKind.Utc);
			Assert.IsTrue(tz.IsDaylightSavingTime(dt));
			cnv = TimeZoneInfo.ConvertTimeToUtc(dt);
			Assert.AreEqual(13, cnv.Hour);
			Assert.AreEqual(4, cnv.Minute);
			Assert.AreEqual(5, cnv.Second);
			Assert.AreEqual(DateTimeKind.Utc, cnv.Kind);

			cnv = TimeZoneInfo.ConvertTimeFromUtc(dt, tz);
			Assert.AreEqual(13 - 6, cnv.Hour);
			Assert.AreEqual(4, cnv.Minute);
			Assert.AreEqual(5, cnv.Second);
			Assert.AreEqual(DateTimeKind.Unspecified, cnv.Kind);
		}
	}
}
